package com.thoughtworks.impersonator.http.replay;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.junit.Test;
import org.mockito.Mockito;

import com.thoughtworks.impersonator.http.message.storage.HttpExchangeStorage;

public class ImpersonatorHandlerTest {

    @Test
    public void shouldReturnResponseFromStorage() throws Exception {
        HttpExchangeStorage storage = mock(HttpExchangeStorage.class);
        MessageEvent event = mock(MessageEvent.class);
        HttpRequest request = mock(HttpRequest.class);
        HttpResponse response = mock(HttpResponse.class);
        Channel channel = mock(Channel.class);
        ChannelFuture channelFuture = mock(ChannelFuture.class);
        
        when(event.getMessage()).thenReturn(request);
        when(event.getChannel()).thenReturn(channel);
        when(channel.write(Mockito.any())).thenReturn(channelFuture);
        
        when(storage.loadResponseFor(request)).thenReturn(response);
        
        ImpersonatorHandler handler = new ImpersonatorHandler(storage);
        handler.messageReceived(mock(ChannelHandlerContext.class), event);
        
        verify(channel).write(response);
    }
}
